﻿/*
 *=====================================================================
 * Name    : AutoEditUI
 * Author  : LCFC RD SS
 * Copyright (c) 2012 - 2019, Hefei LCFC Information Technology Co.Ltd.
 *=====================================================================
 */

using MK;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;

namespace AutoEditUI
{
    class FileWatcher
    {
        public static MainWindow gmainWindow { get; private set; }
        static void WatcherStrat(string path, string filter)
        {
            if (path == null)
            {
                MessageBox.Show("WatcherStrat path == null");
                return;
            }
            else if (!Directory.Exists(  path) )
            {
                MessageBox.Show("WatcherStrat path Not Exsit: "+ path);
                System.Environment.Exit(0);
                return;
            }
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = path;
            watcher.Filter = filter;
            watcher.Changed += new FileSystemEventHandler(OnProcess);
            watcher.Created += new FileSystemEventHandler(OnProcess);
            watcher.Deleted += new FileSystemEventHandler(OnProcess);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);
            watcher.EnableRaisingEvents = true;
            watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
                                   | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            watcher.IncludeSubdirectories = true;
        }
        private static void OnProcess(object source, FileSystemEventArgs e)
        {
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                OnCreated(source, e);
            }
            else if (e.ChangeType == WatcherChangeTypes.Changed)
            {
                OnChanged(source, e);
            }
            else if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                OnDeleted(source, e);
            }
        }

        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            LogHelper.Log("MBL文件新建事件处理逻辑" + e.FullPath + "   " + e.Name);
            FileWork(e);
        }

        private static void FileWork(FileSystemEventArgs e)
        {
            if (e.Name.EndsWith(".txt") || e.Name.EndsWith(".TXT"))
            {
                LogHelper.Log("gmainWindow.Work" + e.ChangeType + " " + e.FullPath + " " + e.Name);
                gmainWindow.Work(e.FullPath);
            }
            else if (e.Name.ToLower().EndsWith(APPConfigure.AckNameType))
            {
                Thread.Sleep(100);
                string xx = e.FullPath.ToLower().Replace(APPConfigure.AckNameType, ".ack");
                LogHelper.Log("gmainWindow.Workack" + e.ChangeType + " " + e.FullPath + " " + e.Name);
                gmainWindow.Work(e.FullPath);
            }
        }

        internal static void WatcherStrat(MainWindow mainWindow, string v1, string v2)
        {
            gmainWindow = mainWindow;
            WatcherStrat(v1, v2);
        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            LogHelper.Log("文件改变事件处理逻辑{0}  {1}  {2}" + e.ChangeType + e.FullPath + e.Name);
        }

        private static void OnDeleted(object source, FileSystemEventArgs e)
        {
            LogHelper.Log("文件删除事件处理逻辑{0}  {1}   {2}" + e.ChangeType + e.FullPath + e.Name);
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            LogHelper.Log("文件重命名事件处理逻辑{0}  {1}  {2}" + e.ChangeType + e.FullPath + e.Name);
            FileWork(e);
        }
    }
}
